const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    phone:'',
    address:'',
    number:'',
    price:'',
    id:'',
    dlid:'',
    levle:'',
    total_price:''
  },

  onShow:function(){
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    console.log("列表页传过来的值",currPage.data);//此处既是上一页面传递过来的值
    this.setData({
      dlid: currPage.data.dlid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('上个页面传过来参数',options)
    this.setData({
      name:options.name,
      phone:options.phone,
      address:options.address,
      number:options.number,
      price:options.price,
      id : options.id,
      dlid: options.dlid,
      levle : options.levle,
      total_price: options.number * options.price
    })  
  },

 
  toPay:function(){
    
    var that = this
    if(that.data.name == '' || that.data.address == ''){
      wx.showToast({
        title: '请选择收货地址',
        image:'../../images/error.png'
      })
    }else{     
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data

          wx.request({
            url: app.globalData.URL + '/app/memberCard/cardNum?number=' + that.data.number + '&openid=' + openid,
            success: function (res) {
              console.log('cardNum', res)
              
              if (res.data.flag == true) {
                wx.request({
                  url: app.globalData.URL + '/app/memberCard/placeOrder?msg=1&id=' + that.data.id + '&number=' + that.data.number + '&levle=' + that.data.levle + '&openid=' + openid + '&deliveryId=' + that.data.dlid,
                  success: function (res) {
                    if (res.data.flag == true) {
                      wx.navigateTo({
                        url: '../memberOrder/memberOrder',
                      })
                    }
                  }
                })
              } else {
                wx.showModal({
                  title: '',
                  content: res.data.msg,
                })
              }
            }
          })
        },
      })
    }
    
  },

  toAddressList:function(){
    wx.navigateTo({
      url: '../address/address?type=1',
    })
  }  
})